<!DOCTYPE html>
<html>
<head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    <meta name="viewport" content="initial-scale=1.0, user-scalable=no" />
    <!-- 页面meta -->
    <meta charset="utf-8">
    <!--<meta http-equiv="X-UA-Compatible" content="IE=edge">-->
    <title>传智健康</title>
    <meta name="description" content="传智健康">
    <meta name="keywords" content="传智健康">
    <meta content="width=device-width,initial-scale=1,maximum-scale=1,user-scalable=no" name="viewport">
    <!-- 引入样式 -->
    <link rel="stylesheet" href="../plugins/elementui/index(1).css">
    <link rel="stylesheet" href="../plugins/font-awesome/css/font-awesome.min.css">
    <link rel="stylesheet" href="../css/style.css">
    <!-- 引入组件库 -->
    <script type="text/javascript" src="../js/jquery.min.js"></script>
    <script src="../js/vue.js"></script>
    <script src="../plugins/elementui/index.js"></script>
    <script src="../js/axios-0.18.0.js"></script>
    <script type="text/javascript" src="//api.map.baidu.com/api?v=2.0&ak=HSmOKmqXVYb0Zt6DYyyUcmWW7hFRR4Ky"></script>
    <style>

        .avatar-uploader .el-upload {
            border: 1px dashed #d9d9d9;
            border-radius: 6px;
            cursor: pointer;
            position: relative;
            overflow: hidden;
        }
        .avatar-uploader .el-upload:hover {
            border-color: #409EFF;
        }
        .avatar-uploader-icon {
            font-size: 28px;
            color: #8c939d;
            width: 178px;
            height: 178px;
            line-height: 178px;
            text-align: center;
        }
        .avatar {
            width: 178px;
            height: 178px;
            display: block;
        }
        .datatable {
            position: relative;
            box-sizing: border-box;
            -webkit-box-flex: 1;
            width: 100%;
            max-width: 100%;
            font-size: 14px;
            color: rgb(96, 98, 102);
            overflow: hidden;
            flex: 1 1 0%;
        }
        .datatable td, .datatable th {
            padding: 12px 0;
            min-width: 0;
            -webkit-box-sizing: border-box;
            box-sizing: border-box;
            text-overflow: ellipsis;
            vertical-align: middle;
            position: relative;
            text-align: left;
        }
        .white_content {
                     display: none;
                     position: absolute;
                     top: 10%;
                     right: 300px;
                     width: 500px;
                     height: 500px;
                     padding: 20px;
                     z-index: 9999;
                     overflow: auto;
                 }

        body, html {
            width: 100%;
            height: 100%;
            margin: 0;
            font-family: "微软雅黑";
        }

    </style>
</head>
<body class="hold-transition">
<div id="app">
    <div class="content-header">
        <h1>体检机构管理<small>体检机构地址管理</small></h1>
        <el-breadcrumb separator-class="el-icon-arrow-right" class="breadcrumb">
            <el-breadcrumb-item :to="{ path: '/' }">首页</el-breadcrumb-item>
            <el-breadcrumb-item>体检机构管理</el-breadcrumb-item>
            <el-breadcrumb-item>体检机构地址管理</el-breadcrumb-item>
        </el-breadcrumb>
    </div>

    <div class="app-container">
        <div class="box">
            <div class="filter-container">
                <el-input placeholder="序号/名称" v-model="pagination.queryString" style="width: 200px;" class="filter-item" @keyup.enter.native="handleFilter"></el-input>
                <el-button @click="findPage()" class="dalfBut">查询</el-button>
                <el-button type="primary" class="butT" @click="handleCreate()">新建</el-button>
            </div>
            <el-table size="small" current-row-key="id" :data="dataList" stripe highlight-current-row>
                <el-table-column type="index" align="center" label="序号"></el-table-column>
                <el-table-column prop="name" label="体检机构名" align="center"></el-table-column>
                <el-table-column prop="telephone" label="联系电话" align="center"></el-table-column>
                <el-table-column label="经纬度" align="center" prop="address"></el-table-column>
                <el-table-column label="机构图片" align="center" prop="img">
                    <template slot-scope="scope">
                        <img :src="scope.row.img" height="40" min-width="40"/>
                    </template>
                </el-table-column>
                <el-table-column prop="remark" label="详细地址" align="center"></el-table-column>
                <el-table-column label="操作" align="center">
                    <template slot-scope="scope">
                        <el-button type="primary" size="mini" @click="handleUpdate(scope.row)">编辑</el-button>
                        <el-button size="mini" type="danger" @click="handleDelete(scope.row)">删除</el-button>
                    </template>
                </el-table-column>
            </el-table>
            <div class="pagination-container">
                <el-pagination
                        class="pagiantion"
                        @current-change="handleCurrentChange"
                        :current-page="pagination.currentPage"
                        :page-size="pagination.pageSize"
                        layout="total, prev, pager, next, jumper"
                        :total="pagination.total">
                </el-pagination>
            </div>
            <!-- 新增标签弹层 -->
            <div class="add-form">
                <el-dialog title="新增体检机构地址" :visible.sync="dialogFormVisible">
                            <el-form ref="dataAddForm" :model="formData" label-position="right" label-width="100px" :rules="rules">
                                <el-row>
                                    <el-col :span="12">
                                        <el-form-item label="体检机构名" prop="name">
                                            <el-input v-model="formData.name"/>
                                        </el-form-item>
                                    </el-col>
                                    <el-col :span="12">
                                        <el-form-item label="联系电话" prop="telephone">
                                            <el-input v-model="formData.telephone"/>
                                        </el-form-item>
                                    </el-col>
                                </el-row>
                                <el-row>
                                    <el-col :span="24">
                                        <el-form-item label="上传地址图片">
                                            <el-upload
                                                    class="avatar-uploader"
                                                    action="/address/upload.do"
                                                    :auto-upload="autoUpload"
                                                    name="imgFile"
                                                    :show-file-list="false"
                                                    :on-success="handleAvatarSuccess"
                                                    :before-upload="beforeAvatarUpload">
                                                <img v-if="imageUrl" :src="imageUrl" class="avatar">
                                                <i v-else class="el-icon-plus avatar-uploader-icon"></i>
                                            </el-upload>
                                        </el-form-item>
                                    </el-col>
                                </el-row>
                                <el-row>
                                    <el-col :span="24">
                                        <el-form-item label="经纬度" >
                                            <el-input v-model="address"/>
                                        </el-form-item>
                                    </el-col>
                                </el-row>
                                <el-row>
                                    <el-col :span="12">
                                        <el-form-item label="详细地址">
                                            <el-input v-model="detailAddress"/>
                                        </el-form-item>
                                    </el-col>
                                    <el-col :span="5">
                                            <a href="javascript:void(0)" onclick="$('#allmap').show();loc()">设置定位</a>
                                    </el-col>
                                </el-row>
                            </el-form>
                    <div slot="footer" class="dialog-footer">
                        <el-button @click="dialogFormVisible = false">取消</el-button>
                        <el-button type="primary" @click="handleAdd()">确定</el-button>
                    </div>
                </el-dialog>
            </div>
            <!-- 编辑标签弹层 -->
            <div class="add-form">
                <el-dialog title="编辑体检机构地址" :visible.sync="dialogFormVisible4Edit">
                    <el-form ref="dataEditForm" :model="formData" :rules="rules" label-position="right" label-width="100px">
                        <el-row>
                            <el-col :span="12">
                                <el-form-item label="体检机构名" prop="name">
                                    <el-input v-model="formData.name"/>
                                </el-form-item>
                            </el-col>
                            <el-col :span="12">
                                <el-form-item label="联系电话" prop="telephone">
                                    <el-input v-model="formData.telephone"/>
                                </el-form-item>
                            </el-col>
                        </el-row>
                        <el-row>
                            <el-col :span="24">
                                <el-form-item label="上传地址图片">
                                    <el-upload
                                            class="avatar-uploader"
                                            action="/checksetmeal/upload.do"
                                            :auto-upload="autoUpload"
                                            name="imgFile"
                                            :show-file-list="false"
                                            :on-success="handleAvatarSuccess"
                                            :before-upload="beforeAvatarUpload">
                                        <img v-if="imageUrl" :src="imageUrl" class="avatar">
                                        <i v-else class="el-icon-plus avatar-uploader-icon"></i>
                                    </el-upload>
                                </el-form-item>
                            </el-col>
                        </el-row>
                        <el-row>
                            <el-col :span="24">
                                <el-form-item label="经度">
                                    <el-input v-model="address"/>
                                </el-form-item>
                            </el-col>
                        </el-row>
                        <el-row>
                            <el-col :span="12">
                                <el-form-item label="详细地址">
                                    <el-input v-model="detailAddress"/>
                                </el-form-item>
                            </el-col>
                            <el-col :span="5">
                                <a href="javascript:void(0)" onclick="$('#allmap').show();loc()">设置定位</a>
                            </el-col>
                        </el-row>
                    </el-form>
                    <div slot="footer" class="dialog-footer">
                        <el-button @click="dialogFormVisible4Edit = false">取消</el-button>
                        <el-button type="primary" @click="handleEdit()">确定</el-button>
                    </div>
                </el-dialog>
            </div>
        </div>
    </div>
    <div id="allmap" class="white_content"></div>
</div>
<script>
    var vue = new Vue({
        el: '#app',
        data:{
            autoUpload:true,//自动上传
            imageUrl:null,//模型数据，用于上传图片完成后图片预览
            pagination: {//分页相关属性
                currentPage: 1,
                pageSize:10,
                total:100,
                queryString:null,
            },
            dataList: [],//列表数据
            formData: {},//表单数据
            detailAddress:null,
            Address:null,
            dialogFormVisible: false,//控制添加窗口显示/隐藏
            dialogFormVisible4Edit:false,//编辑表单是否可见
            rules: {//校验规则
                name: [{ required: true, message: '机构名为必填项', trigger: 'blur' }],
                telephone: [{ required: true, message: '联系电话为必填项', trigger: 'blur' }]
            }
        },
        created() {
            this.findPage();
        },
        methods: {
            //文件上传成功后的钩子，response为服务端返回的值，file为当前上传的文件封装成的js对象
            handleAvatarSuccess(response, file) {
                this.imageUrl = "http://q6nhxtv5b.bkt.clouddn.com/"+response.data;
                this.$message({
                    message: response.message,
                    type: response.flag ? 'success' : 'error'
                });
                //设置模型数据（图片名称），后续提交ajax请求时会提交到后台最终保存到数据库
                this.formData.img = response.data;
            },
            //上传图片之前执行
            beforeAvatarUpload(file) {
                const isJPG = file.type === 'image/jpeg';
                const isLt2M = file.size / 1024 / 1024 < 2;
                if (!isJPG) {
                    this.$message.error('上传套餐图片只能是 JPG 格式!');
                }
                if (!isLt2M) {
                    this.$message.error('上传套餐图片大小不能超过 2MB!');
                }
                return isJPG && isLt2M;
            },
            //编辑
            handleEdit() {
                this.$refs['dataEditForm'].validate((valid)=>{
                    if (valid){
                        axios.post("/address/edit.do",this.formData).then((res)=>{
                            if (res.data.flag){
                                this.$message({
                                    type:"success",
                                    message:res.data.message
                                });
                            } else {
                                this.$message.error(res.data.message)
                            }
                        }).finally(()=>{
                            this.findPage();
                            this.dialogFormVisible4Edit = false;
                        })
                    } else {
                        this.$message.error("表单校验失败，请重新填写");
                        //结束校验
                        return false;
                    }
                })
            },
            //添加
            handleAdd () {
                this.formData.address = this.address;
                this.formData.remark = this.detailAddress;
                this.$refs['dataAddForm'].validate((valid)=>{
                    if(valid){
                        //表单校验成功，将数据通过异步请求发送
                        axios.post("/address/add.do",this.formData).then(function (res) {
                            //关闭新增窗口
                            vue.dialogFormVisible = false;
                            if(res.data.flag){

                                //弹出提示信息
                                vue.$message({
                                    message:res.data.message,
                                    type:"success"
                                })
                            }else{
                                vue.$message.error(res.data.message)
                            }
                        }).finally(()=>{
                            //添加完成后调用最新的数据
                            vue.findPage();
                        })

                    }else {
                        //校验不通过
                        this.$message.error("数据校验失败，请检查你的数据是否正确");
                        return false;
                    }
                })
            },
            //分页查询
            findPage() {
                axios.post("/address/findPage.do",this.pagination).then((res)=>{
                    this.dataList = res.data.rows;
                    this.pagination.total = res.data.total;
                })
            },
            // 重置表单
            resetForm() {
                this.formData = {};
                this.imageUrl = null;
            },
            //切换页码
            handleCurrentChange(currentPage) {
                this.pagination.currentPage = currentPage;
                this.findPage();
            },
            // 弹出添加窗口
            handleCreate() {
                this.resetForm();
                this.dialogFormVisible = true;
            },
            // 弹出编辑窗口
            handleUpdate(row) {
                this.dialogFormVisible4Edit = true;
                axios.get("/address/findById.do?id="+row.id).then((res)=>{
                    if (res.data.flag){
                        //进行回显，基于vue的双向绑定
                        this.formData = res.data.data;
                        this.imageUrl = res.data.data.img;
                        this.address = res.data.data.address;
                        this.detailAddress = res.data.data.remark;
                    } else {
                        this.$message.error("获取数据失败，请刷新当前页面")
                    }

                })
            },
            // 删除
            handleDelete(row) {
                //alert(row.id);
                this.$confirm("确认删除吗？","提示",{type:'warning'}).then(()=>{
                    //点击确定按钮时只需此处代码
                    //alert('用户点击的是确定按钮');
                    axios.get("/address/deleteAddress.do?id=" + row.id).then((res)=> {
                        if(!res.data.flag){
                            //删除失败
                            this.$message.error(res.data.message);
                        }else{
                            //删除成功
                            this.$message({
                                message: res.data.message,
                                type: 'success'
                            });
                            //调用分页，获取最新分页数据
                            this.findPage();
                        }
                        //请求失败，安全框架返回异常
                    }).catch((res)=>{
                        this.showMessage(res)
                    })
                }).catch(()=>{
                    this.$message("已取消操作")
                });
            }
        }
    });
    // 百度地图API功能
    var map = new BMap.Map("allmap");
    map.centerAndZoom(new BMap.Point(116.404, 39.915), 11);
    var geocoder = new BMap.Geocoder();

    function showInfo(e) {
        vue.address = e.point.lng + "," + e.point.lat;
        geocoder.getLocation(e.point, function (rs) {
            vue.detailAddress = rs.address+"";
        });
        $("#allmap").hide();
        /*console.log(rs.address); //地址描述(string)
        alert(rs.address);
        console.log(rs.addressComponents);//结构化的地址描述(object)
        console.log(rs.addressComponents.province); //省
        console.log(rs.addressComponents.city); //城市
        console.log(rs.addressComponents.district); //区县
        console.log(rs.addressComponents.street); //街道
        console.log(rs.addressComponents.streetNumber); //门牌号
        console.log(rs.surroundingPois); //附近的POI点(array)
        console.log(rs.business); //商圈字段，代表此点所属的商圈(string)*/
    }

    function loc() {
        var geolocation = new BMap.Geolocation();
        geolocation.getCurrentPosition(function (r) {
            if (this.getStatus() == BMAP_STATUS_SUCCESS) {
                var mk = new BMap.Marker(r.point);
                map.addOverlay(mk);
                map.panTo(r.point);
                vue.address = r.point.lng + "," + r.point.lat;
                // alert('您的位置：' + r.point.lng + ',' + r.point.lat);
            } else {
                alert('failed' + this.getStatus());
            }
        }, {enableHighAccuracy: true})
    }

    var top_left_control = new BMap.ScaleControl({anchor: BMAP_ANCHOR_TOP_LEFT});// 左上角，添加比例尺
    var top_left_navigation = new BMap.NavigationControl();  //左上角，添加默认缩放平移控件
    var top_right_navigation = new BMap.NavigationControl({
        anchor: BMAP_ANCHOR_TOP_RIGHT,
        type: BMAP_NAVIGATION_CONTROL_SMALL
    });
    //右上角，仅包含平移和缩放按钮
    /*缩放控件type有四种类型:
    BMAP_NAVIGATION_CONTROL_SMALL：仅包含平移和缩放按钮；BMAP_NAVIGATION_CONTROL_PAN:仅包含平移按钮；BMAP_NAVIGATION_CONTROL_ZOOM：仅包含缩放按钮*/
    //添加控件和比例尺
    map.addControl(top_left_control);
    map.addControl(top_left_navigation);
    map.addControl(top_right_navigation);
    map.addEventListener("click", showInfo);

</script>
</body>
</html>
